import {
  Column,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  OneToMany,
} from 'typeorm';
import { BaseEntity } from '../../../common/baseEntity/index';
import { Tag } from '../../tag/entities/tag.entity';
import { Classification } from '../../classification/entities/classification.entity';
import { ArticleLike } from './article-like.entity'
import { ArticleComment } from './article-comment.entity';


@Entity('article')
export class Article extends BaseEntity {
  @Column({ type: 'varchar', length: 255, comment: '标题' })
  title: string;

  @Column({ type: 'tinyint', name: 'is_top', default: 0, comment: '是否置顶' })
  isTop: number;

  @Column({ type: 'tinyint', nullable: true, comment: '文章状态。1正常，0删除，2草稿', default: 1 })
  status: number;

  @Column({ type: 'varchar', length: 255, nullable: true, comment: '关键词' })
  keywords: string;

  @Column({ type: 'varchar', nullable: true, length: 255, comment: '描述' })
  description: string;

  @Column({ type: 'text', nullable: true, comment: '文章内容' })
  content: string;

  @Column({ type: 'varchar', nullable: true, length: 255, comment: '封面图' })
  cover: string;

  @Column({
    type: 'int',
    name: 'read_num',
    default: 0,
    nullable: true,
    comment: '阅读量',
  })
  readNum: number;

  @Column({
    type: 'int',
    name: 'like_num',
    default: 0,
    nullable: true,
    comment: '点赞数',
  })
  likeNum: number;

  @Column({
    type: 'int',
    name: 'comment_num',
    default: 0,
    nullable: true,
    comment: '评论数',
  })
  commentNum: number;

  // 关联分类表
  @JoinColumn({ name: 'classification_id' })
  @ManyToOne(() => Classification, (classification) => classification.articles, {
    cascade: true /**保存文章级联保存这个分类 */,
    onDelete: 'CASCADE', // 只要删了Classification, 就把它们的外键 id 置为空。
  })
  classification: Classification;

  // 关联标签表
  @JoinTable()
  @ManyToMany(() => Tag, { cascade: true, onDelete: 'CASCADE' })
  tags: Tag[];

  // 一篇文章对应多个点赞
  @OneToMany(() => ArticleLike, articleLike => articleLike.article)
  likes: ArticleLike[];

  // 一篇文章对应多个评论
  @OneToMany(() => ArticleComment, comment => comment.article)
  comments: ArticleComment[];
}
